﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Xml;
using System.Windows.Forms;
using System.IO;
using System.Reflection;
using System.Diagnostics;

using FCNS.KeepMoney.Configuration;
using FCNS.KeepMoneyLibrary.Data;

/*显示最近1年的对账单
 * 1.搜索账单样式目录 *.html 文件
 * 如果不存在样式文件则从资源文件中加载默认样式
 * 必须在需要 html 文件中添加此标记 <!--keepmoney--> 用于加载数据 money,name,date,note
 * 
*/
namespace FCNS.KeepMoney.Util
{
    /// <summary>
    /// 账单类
    /// </summary>
    class BillUtil
    {
        /// <summary>
        /// 生成对账单
        /// </summary>
        public static void GenerateBilling(DateTime YearMonth)
        {
            //if (!Directory.Exists(AppDefines.billDirectory))
            //{
            //    try { Directory.CreateDirectory(AppDefines.billDirectory); }
            //    catch (Exception error) { MessageBox.Show("GenerateBilling/r/n" + error.Message); }
            //}
            StringBuilder html;

            if (File.Exists(AppDefines.billDirectory + Program.AppConfig.Bill.BillStyle))
                html = GenerateBillByFile();
            else
                html = GenerateDefaultBill();

            html.Replace("<!--keepmoney-->", GetDataToJS(YearMonth));

            string file = AppDefines.appPath + "bill.html";
            File.WriteAllText(file, html.ToString(), Encoding.UTF8);
            System.Diagnostics.Process.Start(file);
        }

        private static string GetDataToJS(DateTime date)
        {
            StringBuilder js = new StringBuilder("<script language=\"javascript\" type=\"text/javascript\">");
            js.Append("\r\n");
            js.Append("function Entry(money,name,date,note) {");
            js.Append("\r\n");
            js.Append(" var _this = this;");
            js.Append("\r\n");
            js.Append("  _this.Money =money;");
            js.Append("\r\n");
            js.Append(" _this.Name = name;");
            js.Append("\r\n");
            js.Append("  _this.Date = date;");
            js.Append("\r\n");
            js.Append(" _this.Note = note;}");
            js.Append("\r\n");

            DateTime dte,dts;
            if (Program.AppConfig.Bill.BillMode == BillsMode.Month)
            {
                dte = date.AddMonths(1);
               dts = date;
            }
            else
            {
               dte = date.AddDays(Program.AppConfig.Bill.BillDay);
               dts = dte.AddMonths(-1);
            }

            js.Append("array = new Array(");
            decimal p = 0, m = 0;
            StringBuilder sb = new StringBuilder();
            foreach (EntryField field in EntryField.GetEntryField.FindAll(ef=>{return ef.Date>=dts && ef.Date<=dte;}))
            {
                sb.Remove(0, sb.Length);
                sb.Append("new Entry(");
                sb.Append(field.columnMoney);
                sb.Append(",\"");

                sb.Append(field.columnName);
                sb.Append("\",");

                sb.Append("\"" + field.Date.ToString(AppDefines.dateFormat_yyyy_MM_dd) + "\"");
                sb.Append(",\"");

                sb.Append(field.columnNote);
                sb.Append("\"),");
                js.Append(sb);

                if (field.Money>0)
                    p += field.Money;
                else
                    m += field.Money;

            }
            js.Remove(js.Length - 1, 1);
            js.Append(");");
            js.Append("\r\n");
            js.Append("BillDate =\"" + dte.ToString("yyyy - MM") + "\";");
            js.Append("Income =" + p + ";");
            js.Append("\r\n");
            js.Append("Expense =" + m + ";");
            js.Append("\r\n");
            js.Append("</script>");

            return js.ToString();
        }

        private static StringBuilder GenerateBillByFile()
        {
            StreamReader reader = new StreamReader(AppDefines.billDirectory + Program.AppConfig.Bill.BillStyle);
            StringBuilder html = new StringBuilder(reader.ReadToEnd());
            reader.Dispose();
            return html;
        }

        private static StringBuilder GenerateDefaultBill()
        {
            Assembly asm = Assembly.GetExecutingAssembly();
            StreamReader reader = new StreamReader(asm.GetManifestResourceStream("FCNS.KeepMoney.BillStyle.cgb.html"));
            StringBuilder html = new StringBuilder(reader.ReadToEnd());
            reader.Dispose();
            return html;
        }
    }
}
